package com.common.dingding.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.dingding.entity.SchedulingRecord;
import com.common.dingding.mapper.SchedulingRecordMapper;
import com.common.dingding.service.SchedulingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 排班记录Service实现类
 */
@Service
public class SchedulingRecordServiceImpl extends ServiceImpl<SchedulingRecordMapper, SchedulingRecord> implements SchedulingRecordService {
    
    @Autowired
    private SchedulingRecordMapper schedulingRecordMapper;
    
    @Override
    public boolean saveScheduling(SchedulingRecord record) {
        return this.save(record);
    }
    
    @Override
    public boolean saveBatchScheduling(List<SchedulingRecord> records) {
        return this.saveBatch(records);
    }
    
    @Override
    public boolean deleteScheduling(String userId, LocalDate scheduleDate, String deleteBy) {
        // 先查询对应的记录
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("user_id", userId);
        queryParams.put("schedule_date", scheduleDate);
        List<SchedulingRecord> records = this.listByMap(queryParams);
        
        if (records.isEmpty()) {
            return true; // 没有记录，视为删除成功
        }
        
        // 设置删除人
        SchedulingRecord record = records.get(0);
        record.setDeleteBy(deleteBy);
        
        // 更新删除人信息
        this.updateById(record);
        
        // 删除记录
        Map<String, Object> deleteParams = new HashMap<>();
        deleteParams.put("id", record.getId());
        return this.removeByMap(deleteParams);
    }
    
    @Override
    public List<SchedulingRecord> getSchedulingByDate(LocalDate scheduleDate, List<String> userIds) {
        return schedulingRecordMapper.selectByDateAndUserIds(scheduleDate, userIds);
    }
    
    @Override
    public Map<String, Integer> getDailyCountByMonth(int year, int month, List<String> userIds) {
        List<Map<String, Object>> result = schedulingRecordMapper.selectDailyCountByMonth(year, month, userIds);
        Map<String, Integer> dailyCount = new HashMap<>();
        
        // 初始化当月所有日期的计数为0
        LocalDate firstDay = LocalDate.of(year, month, 1);
        LocalDate lastDay = firstDay.plusMonths(1).minusDays(1);
        LocalDate current = firstDay;
        while (!current.isAfter(lastDay)) {
            dailyCount.put(current.toString(), 0);
            current = current.plusDays(1);
        }
        
        // 填充实际的计数
        for (Map<String, Object> map : result) {
            String date = map.get("date").toString();
            Integer count = Integer.parseInt(map.get("count").toString());
            dailyCount.put(date, count);
        }
        
        return dailyCount;
    }
    
    @Override
    public LocalDate getNextScheduleDate(String userId) {
        LocalDate currentDate = LocalDate.now();
        return schedulingRecordMapper.selectNextScheduleDate(userId, currentDate);
    }
    
    @Override
    public boolean isScheduled(String userId, LocalDate scheduleDate) {
        int count = schedulingRecordMapper.selectCountByUserIdAndDate(userId, scheduleDate);
        return count > 0;
    }
}